package edu.wit.acl.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import edu.wit.acl.entity.Project;
import edu.wit.acl.entity.UserProject;
import edu.wit.acl.mapper.ProjectMapper;
import edu.wit.acl.service.ProjectService;
import edu.wit.acl.service.UserProjectService;
import edu.wit.acl.service.UserService;
import edu.wit.common.utils.MarkUpParser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;


@Service
public class ProjectServiceImpl extends ServiceImpl<ProjectMapper, Project> implements ProjectService {


    @Autowired
    private UserService userService;

    @Autowired
    private ProjectService projectService;

    @Autowired
    private UserProjectService userProjectService;

    @Autowired
    private ProjectMapper projectMapper;
    @Override
    public List<Project> selectProjectByUserId(String id) {
        //根据用户id获取的项目id
        List<UserProject> userProjectList = userProjectService.list(new QueryWrapper<UserProject>().eq("user_id", id).select("project_id"));
        List<String> projectIdList = userProjectList.stream().map(item -> item.getProjectId()).collect(Collectors.toList());
        List<Project> projectList = new ArrayList<>();
        if (projectIdList.size() > 0) {
            projectList = baseMapper.selectBatchIds(projectIdList);
        }
        List<Project> ans = new ArrayList<>();
        for(Project project:projectList){
            if(project.getStatus().equals("0")){
                project.setIntroduction(MarkUpParser.getContent(project.getIntroduction()));
                ans.add(project);
            }
        }
        return ans;
    }


    @Override
    public List<Project> selectProjectByProjectIds(List<String> projectIds) {
        List<Project> projectList=new ArrayList<>();
        projectIds.forEach(projectId->{
            Project project = baseMapper.selectOne(new QueryWrapper<Project>().eq("id", projectId).eq("status", 0));
            project.setIntroduction(MarkUpParser.getContent(project.getIntroduction()));
            projectList.add(project);
        });
        return projectList;
    }

    @Override
    public Project selectProjectByProjectName(String projectName){
        return baseMapper.selectOne(new QueryWrapper<Project>().eq("name",projectName));
    }

    @Override
    public Project selectProjectByProjectId(String projectId) {
        return baseMapper.selectById(projectId);
    }

    @Override
    public void deleteProjectById(String projectId) {
        projectMapper.deleteProjectById(projectId);
    }
}
